package demo
{
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.Contacts.b2Contact;
	import Box2D.Dynamics.Contacts.b2ContactEdge;
	import Box2D.Dynamics.Controllers.b2BuoyancyController;
	import Box2D.Dynamics.Controllers.b2ControllerEdge;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2Fixture;
	import Box2D.Dynamics.b2FixtureDef;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class DemoScene3Buoyancy extends DemoScene2BodyEvent
	{
		private var fuildcontroller:b2BuoyancyController;
		public function DemoScene3Buoyancy(s:Sprite)
		{
			super(s);
			
			fuildcontroller = new b2BuoyancyController();
			//设置水面的法向量
			fuildcontroller.normal.Set(0, -1);
			//水位
			fuildcontroller.offset = -350 * m_physScale;
			//使用密度
//			fuildcontroller.useDensity = true;
			//使用世界引力
//			fuildcontroller.useWorldGravity = true;
			//密度
			fuildcontroller.density = 50;
			//移动阻力
			fuildcontroller.linearDrag = 5;
			//旋转阻力
			fuildcontroller.angularDrag = 5;
			
			m_world.AddController(fuildcontroller);
			
			//水池
			//创建刚体的数据模型及位置
			var my_body:b2BodyDef = new b2BodyDef();
			my_body.position.Set(500 * m_physScale, 375 * m_physScale);
			my_body.type = b2Body.b2_staticBody;
			//创建刚体的形状
			var my_box:b2PolygonShape = new b2PolygonShape();
			my_box.SetAsBox(300 / 2 * m_physScale, 150 / 2 * m_physScale);
			//创建刚体的物理参数
			var my_fixture:b2FixtureDef = new b2FixtureDef();
			my_fixture.density = 50;
			my_fixture.friction = 1;
			my_fixture.restitution = 0.5;
			my_fixture.shape = my_box;
			my_fixture.isSensor = true;
			//在物理世界初始化这个刚体并得到它的刚体对象
			var pool:b2Body = m_world.CreateBody(my_body);
			pool.CreateFixture(my_fixture);
		}
		
		override protected function run(e:Event):void{
			super.run(e);
			//该方式是简化后的方式，也可以通过循环世界看列表判断坐标来实现该功能
			//循环世界列表
			for (var currentBody:b2Body=m_world.GetBodyList(); currentBody; currentBody=currentBody.GetNext()) {
				if (currentBody.GetType()==b2Body.b2_dynamicBody) {
					//获取并判断当前body是否存在水箱控制器中
					var currentBodyControllers:b2ControllerEdge=currentBody.GetControllerList();
					if (currentBodyControllers!=null) {
						fuildcontroller.RemoveBody(currentBody);
					}
					//循环碰撞关系列表
					for (var c:b2ContactEdge=currentBody.GetContactList(); c; c=c.next) {
						var contact:b2Contact=c.contact;
						var fixtureA:b2Fixture=contact.GetFixtureA();
						var fixtureB:b2Fixture=contact.GetFixtureB();
						//判断 传感器，如果有一方是水箱控制器的（传感器），则将另一方加入水箱控制器
						if (fixtureA.IsSensor()) {
							var bodyB:b2Body=fixtureB.GetBody();
							var bodyBControllers:b2ControllerEdge=bodyB.GetControllerList();
							if (bodyBControllers==null) {
								fuildcontroller.AddBody(bodyB);
							}
						} else if (fixtureB.IsSensor()) {
							var bodyA:b2Body=fixtureA.GetBody();
							var bodyAControllers:b2ControllerEdge=bodyA.GetControllerList();
							if (bodyAControllers==null) {
								fuildcontroller.AddBody(bodyA);
							}
						}
					}
				}
			}
		}
		override protected function clickHandler(e:MouseEvent):void{
			super.clickHandler(e);
		}
	}
}